package com.honey.solr;

import java.util.List;

import org.java.plugin.PluginLifecycleException;

import com.honey.core.CorePlugin;
import com.honey.core.calculator.JavaNameCalculator;
import com.honey.core.notify.Signal;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.utils.ObjectFactory;
import com.honey.solr.analyzer.Analyzer;
import com.honey.solr.analyzer.GeneralAnalyzer;
import com.honey.solr.analyzer.IKAnalyzer;
import com.honey.solr.analyzer.Mmseg4j;
import com.honey.solr.analyzer.SolrAnalyzerNode;

public class SolrFactory {
	
	private SolrPlugin solrPlugin = null;
	
	private SolrAnalyzerNode generalAnalyzer = null;
	
	private String solrTextType = null;
	
	private JavaNameCalculator javaNameCalculator = null;
	
	public SolrFactory (SolrPlugin solrPlugin ){
		this .solrPlugin =solrPlugin;
		if(solrPlugin.getAnalyzer() != Analyzer.GENERAL){
			generalAnalyzer = new GeneralAnalyzer();
		}
		
		solrTextType = SolrConstant.SOLR_SCHEMA_GENERAL_FIELD_TYPE;
		if(solrPlugin.getAnalyzer() != Analyzer.GENERAL){
			solrTextType = SolrConstant.SOLR_SCHEMA_CHINESE_FIELD_TYPE;
		}
		
		try {
			CorePlugin corePlugin = (CorePlugin)ObjectFactory.getRequestPlugin(solrPlugin, "honey.core.plugin");
			javaNameCalculator = corePlugin.getJavaNameCalculator();
			
		} catch (PluginLifecycleException e) {
			e.printStackTrace();
		}
	} 
	
	/**
	 * 获取建立索引的列
	 * @return
	 */
	public List<SchemaField> getSchemaFields(){
		return solrPlugin.getSchemaFields();
	}
	
	/**
	 * 获取默认的词法分析器
	 * @return
	 */
	public SolrAnalyzerNode getGeneralAnalyzer(){
		return generalAnalyzer; 
	}
	
	/**
	 * 获取中文词法分析器
	 * @return
	 */
	public SolrAnalyzerNode getChineseAnalyzer(){
		SolrAnalyzerNode answer = null;
		switch( solrPlugin.getAnalyzer()){
		case IKAnalyzer:
			answer = new IKAnalyzer();
			break ;
		case GENERAL:
			answer = new GeneralAnalyzer();
			break ;
		case MMSEG4J :
			answer = new Mmseg4j();
			break ;
		}
		return answer ;
	}
	
	/**
	 * 获取文本内容使用的索引类型
	 * @return
	 */
	public String getSolrTextType() {
		return solrTextType;
	}

	public String getUniqueKey(){
		return solrPlugin.getUniqueKey();
	}
	
	public FullyQualifiedJavaType getEntityType(){
	 	String classSuffix = solrPlugin.getTargetEntitySuffix();
		String name =javaNameCalculator.classNameCalculator(solrPlugin.getSchemaName(),classSuffix);
		FullyQualifiedJavaType answer = new FullyQualifiedJavaType(solrPlugin.getTargetEntityPackage(), name);
		return answer ;
	}

	public FullyQualifiedJavaType getModelType(){
	 	String classSuffix = solrPlugin.getTargetModelClassSuffix();
		String name =javaNameCalculator.classNameCalculator(solrPlugin.getSchemaName(),classSuffix);
		FullyQualifiedJavaType answer = new FullyQualifiedJavaType(solrPlugin.getTargetModelPackage(), name);
		return answer ;
	}
	
	public FullyQualifiedJavaType getConditionType(){
		FullyQualifiedJavaType answer = new FullyQualifiedJavaType(solrPlugin.getTargetModelPackage(), "Condition");
		return answer ;
	}
	
	public FullyQualifiedJavaType getGeneratedCriteriaType(){
		FullyQualifiedJavaType answer = new FullyQualifiedJavaType(solrPlugin.getTargetModelPackage(), "GeneratedCriteria");
		return answer ;
	}
	
	public FullyQualifiedJavaType getCriterionType(){
		FullyQualifiedJavaType answer = new FullyQualifiedJavaType(solrPlugin.getTargetModelPackage(), "GeneratedCriteria.Criterion");
		return answer ;
	}
	
	public FullyQualifiedJavaType getModelTableType(){
	 	String classSuffix = solrPlugin.getTargetModelFieldEnumSuffix();
		String name =javaNameCalculator.classNameCalculator(solrPlugin.getSchemaName(),classSuffix);
		FullyQualifiedJavaType answer = new FullyQualifiedJavaType(solrPlugin.getTargetModelPackage(), name);
		return answer ;
	}
	
	
	public FullyQualifiedJavaType getModelSuperClassType(){
		FullyQualifiedJavaType answer = null;
		String classSuffix = solrPlugin.getTargetModelClassSuffix();
		String className =javaNameCalculator.classNameCalculator(solrPlugin.getSchemaName(),classSuffix);
		
		String tableClassSuffix = solrPlugin.getTargetModelFieldEnumSuffix();
		String tableName =javaNameCalculator.classNameCalculator(solrPlugin.getSchemaName(),tableClassSuffix);
		
		String s =solrPlugin.getTargetModelPackage()+".GeneratedCriteria<"+className+", "+solrPlugin.getTargetModelPackage()+"."+className+"."+tableName+">";
		
		answer = new FullyQualifiedJavaType(s);
		
		return answer;
	}
	
	public void snedSignal(Signal signal ) {
		try {
			CorePlugin plugin = (CorePlugin)ObjectFactory.getRequestPlugin(solrPlugin, "honey.core.plugin") ;
			plugin.notify(signal);
		} catch (PluginLifecycleException e) {
			e.printStackTrace();
		}
	}
	
	public JavaNameCalculator getJavaNameCalculator() {
		return javaNameCalculator;
	}
	
	public String getSchemaName(){
		return solrPlugin.getSchemaName();
	}
	
	/**
	 * @return the solrPlugin
	 */
	public final SolrPlugin getSolrPlugin() {
		return solrPlugin;
	}
	
	
}
